<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of PaymentHelp
 * 支付相关的操作
 * @author Administrator
 */
class PaymentHelp {
    /**
     * 获得支付方式列表
     * @return type 返回支付类型列表
     */
    public static function getPayList(){
        $list=array();
        $paymentpath=ROOT.'core/Api/Payment/';
        $dir_hand=  opendir($paymentpath);
        while ($fname=  readdir($dir_hand))
        {
            if($fname=='.'||$fname=='..')
            {
              continue;
            }else{
                $conf_file=$paymentpath.strtolower($fname).'/config.info';
                if(file_exists($conf_file)){
                    $list[strtolower($fname)]=self::read_info($conf_file);
                }else{
                     Debugshow::show($conf_file.'文件不存在');
                }
            }
        }
        return $list;
    }
    //获取支付对象
    public static function loadPayObject($payname,$objectname){
        $paymentpath=ROOT.'core/Api/Payment/';
        $php_file=$paymentpath.strtolower($payname).'/'.strtolower($objectname).".php";
        if(file_exists($php_file)){
            require_once $php_file;
            return new $objectname();
        }else{
             Debugshow::show($php_file.'文件不存在');
             return NULL;
        }
    }

    public static function getPayForm($payname){
        $paymentpath=ROOT.'core/Api/Payment/';
        $conf_file=$paymentpath.strtolower($payname).'/form.info';
        if(file_exists($conf_file)){
            return file_get_contents($conf_file);
        }else{
             Debugshow::show($conf_file.'文件不存在');
             return "";
        }
    }
    /**
     * 保存配置到文件中,从数据库到文件中
     * @param type $data 配置数据
     */
    public static function saveConfig($data){
        $paymentpath=ROOT.'core/Api/Payment/';
        $conf_file=$paymentpath.strtolower($data["base_name"]).'/config.info';
        if(file_exists($conf_file)){
            $f=  fopen($conf_file, "w");
            fwrite($f,"name=".$data["c_base_name"]."\r\n"."main=".$data["main"]."\r\n"."desc=".$data["desc"]."\r\n");
            fclose($f);
               
        }  else {
            Debugshow::show("支付配置文件不存在!");    
        }
    }
    /*
   * 实现读取每个模块下的.info文件
   */
  private static function read_info($filename)
  {
    $ret=array();
    $fhand=  fopen($filename, "r");
    while($line=  fgets($fhand))
    {
        $arr=explode("=", $line);
        $ret[trim($arr[0])]=trim($arr[1]);
    }
    fclose($fhand);
    return $ret;
  }
}
